﻿package net.wangit.jdbc.executable;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.table.Column;
import net.wangit.util.Checker;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


@SuppressWarnings("serial")
public abstract class AbstractColumnRow implements ColumnRow {

	private static final Log log = LogFactory
			.getLog(net.wangit.jdbc.executable.AbstractColumnRow.class);
	private int rowIndex;
	private Map<String, String> columns;
	private Map<String, String> primaryKeyColumns;
	protected String versionColumnValue;

	public AbstractColumnRow() {
		columns = new HashMap<String, String>();
		primaryKeyColumns = new HashMap<String, String>();
	}

	public void setPrimaryKey(String name, String value) {
		if (Checker.isEmpty(value))
			log.warn((new StringBuilder(String.valueOf(name))).append(
					"' value is null").toString());
		primaryKeyColumns.put(name, value);
	}

	public void set(String name, String value) {
		columns.put(name, value);
	}

	public void set(String name, int value) {
		set(name, Integer.toString(value));
	}

	public void set(String name, long value) {
		set(name, Long.toString(value));
	}

	public void set(String name, float value) {
		set(name, Float.toString(value));
	}

	public void set(String name, double value) {
		set(name, Double.toString(value));
	}

	public String get(String name) {
		return columns.get(name);
	}

	public int getInt(String name, int defaultValue) {
		String value = get(name);
		return Checker.isEmpty(value) ? defaultValue : Integer.parseInt(value);
	}

	public long getLong(String name, long defaultValue) {
		String value = get(name);
		return Checker.isEmpty(value) ? defaultValue : Long.parseLong(value);
	}

	public float getFloat(String name, float defaultValue) {
		String value = get(name);
		return Checker.isEmpty(value) ? defaultValue : Float.parseFloat(value);
	}

	public double getDouble(String name, double defaultValue) {
		String value = get(name);
		return Checker.isEmpty(value) ? defaultValue : Double
				.parseDouble(value);
	}

	protected String getColumnValue(Column column) {
		String value = get(column.getName());
		if (column.isEnabledByGenerator(this)) {
			value = column.getGenerator().generate();
			set(column.getName(), value);
		}
		return column.getType().formatValue(value);
	}

	public String getPrimaryKey(String name) {
		return primaryKeyColumns.get(name);
	}

	public void setVersionColumnValue(String versionColumnValue) {
		this.versionColumnValue = versionColumnValue;
	}

	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	public int getRowIndex() {
		return rowIndex;
	}

	public void print() {
		Set<String> keySet;
		Iterator<String> keys;
		String key;
		String value;

		System.out.println("[ColumnRow]-[KeyColumns]:");
		keySet = primaryKeyColumns.keySet();
		keys = keySet.iterator();
		while (keys.hasNext()) {
			key = keys.next();
			value = primaryKeyColumns.get(key);
			System.out.println((new StringBuilder("ColumnName=")).append(key)
					.append(", Value=").append(value).toString());
		}

		System.out.println("[ColumnRow]-[Columns]:");
		keySet = columns.keySet();
		keys = keySet.iterator();
		while (keys.hasNext()) {
			key = keys.next();
			value = columns.get(key);
			System.out.println((new StringBuilder("ColumnName=")).append(key)
					.append(", Value=").append(value).toString());
		}
	}
}
